package com.zzedu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzPictureNode;
import com.zzedu.dto.ZzPictureNodeDTO;
import com.zzedu.service.IZzPictureNodeService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * (教育)绘本节点Controller
 *
 * @author Mr.Wang
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/course/node")
public class ZzPictureNodeController extends BaseController {
    @Autowired
    private IZzPictureNodeService zzPictureNodeService;

    /**
     * 查询(教育)绘本节点列表
     */
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzPictureNodeDTO zzPictureNode) {
        return getDataTable(zzPictureNodeService.page(zzPictureNode.getPageDTO(), zzPictureNodeService.buildQuery(zzPictureNode)));
    }

    /**
     * 导出(教育)绘本节点列表
     */
    @Log(title = "(教育)绘本节点", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzPictureNodeDTO zzPictureNode) {
        List<ZzPictureNode> list = zzPictureNodeService.list(zzPictureNodeService.buildQuery(zzPictureNode));
        ExcelUtil<ZzPictureNode> util = new ExcelUtil<ZzPictureNode>(ZzPictureNode.class);
        util.exportExcel(ServletUtils.getResponse(), list, "(教育)绘本节点数据");
    }


    /**
     * 新增保存(教育)绘本节点
     */
    @Log(title = "(教育)绘本节点", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody ZzPictureNode zzPictureNode) {
        zzPictureNodeService.save(zzPictureNode);
        return AjaxResult.success(zzPictureNode);
    }


    /**
     * 获取(教育)绘本节点详细信息
     */
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zzPictureNodeService.getById(id));
    }


    /**
     * 修改保存(教育)绘本节点
     */
    @Log(title = "(教育)绘本节点", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody ZzPictureNode zzPictureNode) {
        zzPictureNodeService.updateById(zzPictureNode);
        return AjaxResult.success(zzPictureNode);
    }

    /**
     * 删除(教育)绘本节点
     */
    @Log(title = "(教育)绘本节点", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        LambdaQueryWrapper<ZzPictureNode> lambdaQueryWrapper = Wrappers.lambdaQuery(ZzPictureNode.class);
        lambdaQueryWrapper.in(ZzPictureNode::getId, ids);
        lambdaQueryWrapper.or().in(ZzPictureNode::getpNodeId, ids);
        return toAjax(zzPictureNodeService.remove(lambdaQueryWrapper));
    }


    /**
     * 设置顺序
     */
    @Log(title = "(教育)绘本节点", businessType = BusinessType.DELETE)
    @RequestMapping("/setNodeSortNo")
    public AjaxResult setNodeSortNo(@RequestBody List<Long> ids) {
        //根据索集合顺序 设置 sortNo 值
        List<ZzPictureNode> zzPictureNodeList = Lists.newArrayList();
        Long index = 0L;
        for (Long id : ids) {
            ZzPictureNode zzPictureNode = new ZzPictureNode();
            zzPictureNode.setId(id);
            zzPictureNode.setSortNo(index++);
            zzPictureNodeList.add(zzPictureNode);
        }
        return toAjax(zzPictureNodeService.updateBatchById(zzPictureNodeList));
    }

}
